<?php

/**
 * Config common file
 *
 */
// Control de Mensajes de Error
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);

// Constante bandera de acceso
define('JL', 1);

require ('./administration/Libraries/Config.class.php');
require ('./administration/config.php');

define('JLPATH_BASE', basePath());

if (isOldBrowser()) {
    header("Location: /actualiser.html");
    exit;
}

$cnn = mysql_connect($config->get('dbHost'), $config->get('dbUser'), $config->get('dbPass')) or die("Error de Conneccion: " . mysql_error());
mysql_select_db($config->get('dbName')) or die("Error de Conneccion a la DB.");

@mysql_query("SET NAMES UTF8");

function basePath($path = '') {
    if (empty($path))
        $path = $_SERVER['PHP_SELF'];
    $url = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https://' :
            'http://';
    $url .= $_SERVER['HTTP_HOST'] . dirname($path);
    $url = str_replace('/@#', '', $url . '@#');
    $url = str_replace('@#', '', $url);
    return $url;
}

function isLogged() {
    return ($_SESSION['_session_id'] === session_id());
}

function getIpAddress() {
    return (empty($_SERVER['HTTP_CLIENT_IP']) ? (empty($_SERVER['HTTP_X_FORWARDED_FOR']) ?
                            $_SERVER['REMOTE_ADDR'] : $_SERVER['HTTP_X_FORWARDED_FOR']) : $_SERVER['HTTP_CLIENT_IP']);
}

function getNameDomain() {
    $url = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://';
    $url .= $_SERVER['HTTP_HOST'];
    return $url;
}

function isOldBrowser() {
    $old = 0;

    if (!empty($_SERVER['HTTP_USER_AGENT'])) {
        // IE <= 7
        // User Agent: Opera/9.80 (Windows NT 6.1; U; en) Presto/2.10.229 Version/11.61
        if (preg_match('#msie\s+(\d+)\.(\d+)#si', $_SERVER['HTTP_USER_AGENT'], $matches)) {
            if ($matches[1] <= 7) {
                $old = 1;
            }
        }
        // Firefox <= 7
        // User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0.2) Gecko/20100101 Firefox/10.0.2
        elseif (preg_match('#Firefox/(\d+)\.(\d+)[\.\d]+#si', $_SERVER['HTTP_USER_AGENT'], $matches)) {
            if ($matches[1] <= 7) {
                $old = 1;
            }
        }
        // Safari < 5
        // User Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.52.7 (KHTML, like Gecko) Version/5.1.2 Safari/534.52.7
        elseif (preg_match('#Version/(\d+)[\.\d]+ Safari/[\.\d]+#si', $_SERVER['HTTP_USER_AGENT'], $matches)) {
            if ($matches[1] < 5) {
                $old = 1;
            }
        }
        // opera < 11
        // User Agent: Opera/9.80 (Windows NT 6.1; U; en) Presto/2.10.229 Version/11.61
        elseif (preg_match('#Opera/[\.\d]+.*?Version/(\d+)[\.\d]+#si', $_SERVER['HTTP_USER_AGENT'], $matches)) {
            if ($matches[1] < 11) {
                $old = 1;
            }
        }
    }

    return $old;
}